package com.hqyj.javaSpringCloudTest.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.entity.common.Result;
import com.hqyj.entity.common.SearchBean;
import com.hqyj.entity.test.City;
import com.hqyj.javaSpringCloudTest.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：Cwd
 * @date ：2021/8/19 10:54
 */
@RestController
@RequestMapping("/api/test/")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * 127.0.0.1/api/city ---- post
     * {"cityName":"dreamCity","localCityName":"梦幻之城","countryId":"522"}
     * @return
     */
    @PostMapping(value = "/city", consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    }
    /**
     * @author: Cwd
     * @description: 127.0.0.1/api/city ---- put
     * cityName=shanghai,localCityName=上海,cityId=1890
     * @date: 2021/8/19 13:41
     * @return
     */
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }

    /**
     * 127.0.0.1/api/city/2255 ---- delete
     * @param cityId
     * @return
     */
    @DeleteMapping(value = "/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable int cityId){
        return cityService.deleteCityByCityId(cityId);
    }

    /**
     * 127.0.0.1:8761/city/1890
     * @param cityId
     * @return
     */
    @GetMapping("/city/{cityId}")
    public City selectCityById(@PathVariable int cityId){
        return cityService.selectCityById(cityId);
    }

    /**
     * 127.0.0.1:8761/api/cities/522 ---- get
     * @param countryId
     * @return
     */
    @GetMapping("/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId){
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     * 127.0.0.1:8761/api/cities ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"city_name","direction":"desc","keyword":""}
     *                          sql中的排序字段，故用数据库中的字段，不要与驼峰法混淆
     * @param searchBean
     * @return
     */
    @PostMapping(value = "/cities",consumes = "application/json")
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean) {
        return cityService.getCitiesBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1:8761/api/batch/cities ---- post
     * @param cities 参数
     * [{"cityName":"dreamCity1","localCityName":"梦幻之城1","countryId":"522"},
     *  {"cityName":"dreamCity2","localCityName":"梦幻之城2","countryId":"522"}]
     * @return
     */
    @PostMapping(value = "/batch/cities",consumes = "application/json")
    public Result<Object> insertCities(@RequestBody List<City> cities){

        return cityService.insertCities(cities);
    }
}
